﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExtraOpdrachten
{
    public partial class Opdracht11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            int numberOfCharacters = 8;
            Random rnd = new Random();
            string randomPassword = "";
            if(!int.TryParse(txtLength.Text , out numberOfCharacters))
            {
                lblResult.Text = "Je hebt een ongeldige waarde ingevoerd voor de lengte";
                txtLength.Text = "8";
                txtLength.Focus();
            }
            char[] characters = CreateArray();
            for(int i = 0 ; i < numberOfCharacters ; i++)
            {
                randomPassword += characters[rnd.Next(characters.Length)].ToString();
            }
            lblResult.Text = "<i>Het gegenereerde wachtwoord is: <b>" + 
                randomPassword + "</b></i>";
        }

        /// <summary>
        /// Creates an array with digits and uppercase and lowercase letters.
        /// </summary>
        /// <returns></returns>
        protected char[] CreateArray()
        {
            char[] characters = new char[62];
            for(int i = 48 ; i <= 57 ; i++)
            {
                characters[i - 48] = (char)i;
            }

            for (int i = 65; i <= 90; i++)
            {
                characters[i - 65 + 10] = (char)i;
            }

            for (int i = 97; i <= 122; i++)
            {
                characters[i - 97 + 36] = (char)i;
            }
            return characters;
        }

    }
}